﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sec_scan指纹识别系统
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            ReferInfo();
        }
        /// <summary>
        /// 刷新数据
        /// </summary>
        public void ReferInfo()
        {
            string sqlinfo = "data source=Sec_infoscan.db";
            SQLiteConnection dataConn = new SQLiteConnection(sqlinfo);
            dataConn.Open();
            try
            {
                if (dataConn.State == ConnectionState.Open)
                {
                    string sql = "SELECT ID, fingepath, fingekey, fingename, fingeip, systemname, systemversion, systemtype FROM Sec_form";
                    SQLiteCommand command = new SQLiteCommand(sql, dataConn);
                    using (SQLiteDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (dr.Read())
                        {
                            string s1 = dr["ID"].ToString();
                            string s2 = dr["fingepath"].ToString();
                            string s3 = dr["fingekey"].ToString();
                            string s4 = dr["fingename"].ToString();
                            string s5 = dr["fingeip"].ToString();
                            string s6 = dr["systemname"].ToString();
                            string s7 = dr["systemversion"].ToString();
                            string s8 = dr["systemtype"].ToString();
                            ListViewItem item = new ListViewItem();
                            item.Text = s1;
                            item.SubItems.Add(s2);
                            item.SubItems.Add(s3);
                            item.SubItems.Add(s4);
                            item.SubItems.Add(s6);
                            item.SubItems.Add(s7);
                            item.SubItems.Add(s8);
                            listView1.Items.Add(item);
                            this.listView1.Items[this.listView1.Items.Count - 1].EnsureVisible();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("数据库无法打开");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("指纹数据刷新成功！" + ex.ToString());
            }
        }
        /// <summary>
        /// 添加指纹数据
        /// </summary>
        public void AddInfo()
        {
            string sqlinfo = "data source=Sec_infoscan.db";
            SQLiteConnection dataConn = new SQLiteConnection(sqlinfo);
            dataConn.Open();
            try
            {
                if (dataConn.State == ConnectionState.Open)
                {
                    string sql = "INSERT INTO Sec_form (fingepath, fingekey, fingename, systemname, systemversion, systemtype) VALUES( '"+textBox1.Text+ "', '" + textBox2.Text + "', '" + textBox3.Text + "', '" + textBox4.Text + "', '" + textBox5.Text + "', '" + comboBox1.Text + "')";
                    SQLiteCommand command = new SQLiteCommand(sql, dataConn);
                    command.ExecuteReader();
                    MessageBox.Show("数据添加成功！");
                    ReferInfo();
                }
                else
                {
                    MessageBox.Show("数据库无法打开");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("指纹数据刷新成功！" + ex.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("指纹路径不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("指纹关键特征不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show("指纹名称不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(textBox4.Text))
            {
                MessageBox.Show("指纹系统名称不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(textBox5.Text))
            {
                MessageBox.Show("指纹版本不能为空！");
                return;
            }
            AddInfo();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //刷新数据
            ReferWafInfo();
        }
        /// <summary>
        /// 刷新WAF指纹数据
        /// </summary>
        public void ReferWafInfo()
        {
            string sqlinfo = "data source=Sec_infoscan.db";
            SQLiteConnection dataConn = new SQLiteConnection(sqlinfo);
            dataConn.Open();
            try
            {
                if (dataConn.State == ConnectionState.Open)
                {
                    string sql = "SELECT ID, fingewafname, fingewafkey, fingewafform, Serverinfo, PoweredBy FROM Sec_foms";
                    SQLiteCommand command = new SQLiteCommand(sql, dataConn);
                    using (SQLiteDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (dr.Read())
                        {
                            string s1 = dr["ID"].ToString();
                            string s2 = dr["fingewafname"].ToString();
                            string s3 = dr["fingewafkey"].ToString();
                            string s4 = dr["fingewafform"].ToString();
                            string s5 = dr["Serverinfo"].ToString();
                            string s6 = dr["PoweredBy"].ToString();
                            ListViewItem item = new ListViewItem();
                            item.Text = s1;
                            item.SubItems.Add(s2);
                            item.SubItems.Add(s3);
                            item.SubItems.Add(s4);
                            item.SubItems.Add(s5);
                            item.SubItems.Add(s6);
                            listView2.Items.Add(item);
                            this.listView2.Items[this.listView2.Items.Count - 1].EnsureVisible();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("数据库无法打开");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("指纹数据刷新成功！" + ex.ToString());
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox10.Text))
            {
                MessageBox.Show("指纹名称不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(textBox9.Text))
            {
                MessageBox.Show("指纹关键特征不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(textBox8.Text))
            {
                MessageBox.Show("指纹厂家不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(textBox7.Text))
            {
                MessageBox.Show("指纹服务不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(textBox6.Text))
            {
                MessageBox.Show("指纹power不能为空！");
                return;
            }
            //添加WAF指纹
            AddWaf();
        }
        /// <summary>
        /// 添加WAF指纹
        /// </summary>
        public void AddWaf()
        {
            string sqlinfo = "data source=Sec_infoscan.db";
            SQLiteConnection dataConn = new SQLiteConnection(sqlinfo);
            dataConn.Open();
            try
            {
                if (dataConn.State == ConnectionState.Open)
                {
                    string sql = "INSERT INTO Sec_foms (fingewafname, fingewafkey, fingewafform, Serverinfo, PoweredBy) VALUES('"+textBox10.Text+ "', '" + textBox9.Text + "', '" + textBox8.Text + "', '" + textBox7.Text + "', '" + textBox6.Text + "')";
                    SQLiteCommand command = new SQLiteCommand(sql, dataConn);
                    command.ExecuteReader();
                    MessageBox.Show("数据添加成功！");
                    ReferWafInfo();
                }
                else
                {
                    MessageBox.Show("数据库无法打开");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("指纹数据刷新成功！" + ex.ToString());
            }
        }
    }
}
